package com.coffee.minio;

import com.coffee.minio.args.MochaBaseArgs;
import com.coffee.minio.args.MochaPutObjectArgs;
import io.minio.GetObjectArgs;
import io.minio.GetObjectResponse;
import io.minio.GetPresignedObjectUrlArgs;
import io.minio.MinioClient;
import io.minio.http.Method;
import lombok.Getter;
import org.springframework.util.StreamUtils;

import java.io.ByteArrayOutputStream;


/**
 * @author: songkui
 * @since: 2023/5/10 13:59
 */
public class MochaMinioTemplate {

    @Getter
    private final MinioClient minioClient;
    private final MochaMinioProperties minioProperties;

    public MochaMinioTemplate(MinioClient minioClient, MochaMinioProperties minioProperties){
        this.minioClient = minioClient;
        this.minioProperties = minioProperties;
    }

    /**
     *  上传 数据
     * @param putObjectArgs
     * @throws Exception
     */
    public void uploadObject(MochaPutObjectArgs putObjectArgs) throws Exception {
        minioClient.putObject(putObjectArgs.buildMinioArgs());
    }


    /**
     * 下载 数据
     * @param baseArgs
     * @return
     * @throws Exception
     */
    public GetObjectResponse getObject(MochaBaseArgs baseArgs) throws Exception {
        return minioClient.getObject(
                GetObjectArgs.builder()
                        .bucket(baseArgs.getBucket())
                        .object(baseArgs.getObject())
                        .build());
    }

    public byte[] getObjectStream(MochaBaseArgs baseArgs)throws Exception{
        GetObjectResponse objectResponse = null;
        ByteArrayOutputStream outputStream = null;
        byte[] bytes = null;
        try {
            objectResponse = getObject(baseArgs);
            outputStream = new ByteArrayOutputStream();
            StreamUtils.copy(objectResponse, outputStream);
            bytes = outputStream.toByteArray();
            outputStream.close();
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        } finally {
            if (objectResponse != null){
                objectResponse.close();
            }
            if (null != outputStream){
                outputStream.close();
            }
        }
        return bytes;
    }


    /**
     * 得到 url  bucket 必须设置成public
     * 主要用于图片
     * @param baseArgs
     * @return
     */
    public String getPublicURL(MochaBaseArgs baseArgs) {
        return String.format("%s/%s/%s", minioProperties.getEndpoint(), baseArgs.getBucket(), baseArgs.getObject());
    }

    /**
     * 得到 url  bucket 为private 也可以只是有一个失效时间； 过期则该url 不能访问
     * @param baseArgs
     * @return
     * @throws Exception
     */
    public String getPrivateURL(MochaBaseArgs baseArgs) throws Exception {
        return minioClient.getPresignedObjectUrl(
                        GetPresignedObjectUrlArgs.builder()
                                .method(Method.GET)
                                .bucket(baseArgs.getBucket())
                                .object(baseArgs.getObject())
                                .expiry(60 * 60 * 24) //默认一天  不填 默认七天
                                .build());
    }


}
